import java.util.HashMap;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasChildFilter;
import org.htmlparser.filters.StringFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.w3c.dom.Element;

public class ArticleHeader {
	public String _title;
	public String _description;
	public String _url;
	public String _imageUrl;
	public String _pubDate;
	public HashMap<String, String> _links = new HashMap<String, String>();

	public ArticleHeader(Element element) 
	{
		try {
			_title = element.getElementsByTagName("title").item(0).getTextContent();
			Parser parser = new Parser(element.getElementsByTagName("description").item(0).getTextContent());
			NodeList list = parser.parse(null);
			NodeList listImage =  new NodeList();
			NodeFilter filterImage = new AndFilter(new TagNameFilter ("a"),new HasChildFilter (new TagNameFilter ("img")));
			NodeList listLink = new NodeList();
			NodeFilter filterLink = new TagNameFilter("A");
			NodeList listDesc =  new NodeList();
			NodeFilter filterDesc = new StringFilter("");
			for (NodeIterator e = list.elements (); e.hasMoreNodes (); )
			{
				Node node = e.nextNode();
				node.collectInto(listImage, filterImage);
				node.collectInto(listLink, filterLink);
				node.collectInto(listDesc, filterDesc);
			}
			System.out.println(listDesc.size()+ " ...");
			_description = listDesc.elementAt(1).getText();
			_imageUrl = ((org.htmlparser.Tag)listImage.elementAt(0).getFirstChild()).getAttribute("src");
			for (NodeIterator e = listLink.elements (); e.hasMoreNodes (); )
			{
				Node node = e.nextNode();
				String href = ((org.htmlparser.Tag)node).getAttribute("href");
				String content = node.getChildren().elementAt(0).getText();
				if(!content.contains(_imageUrl))
					_links.put(href,content);
			}
			System.out.println(listImage.size() +" "+listLink.size());
			_url = element.getElementsByTagName("link").item(0).getTextContent();
			_pubDate = element.getElementsByTagName("pubDate").item(0).getTextContent();
			System.out.println(_title);
			System.out.println(_description);
			System.out.println(_imageUrl);
			System.out.println(_url);
			System.out.println(_pubDate);
			for(String key : _links.keySet())
			{
				System.out.println(key);
				System.out.println(_links.get(key));
			}

		} catch (ParserException e1) {
			e1.printStackTrace();
		}
	}

	public String getTitle(){ return _title; }
	public String getDescription(){ return _description; }
	public String getUrl(){ return _url; }
	public String getImageUrl(){ return _imageUrl; }
	public String publicationDate(){ return _pubDate; }
	public HashMap<String, String> getLinks(){ return _links; }
}
